﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WeatherReader;
using System.Globalization;

public partial class AllData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
       
        
        var weatherDb =  new WeatherDbDataContext();


        var allReadings = (from h in weatherDb.HouseholdDatas
                              
                              group h by h.Year into annual
                              select new
                              {
                                  Year = annual.Key,
                                  ElectricityCo2 = annual.Sum(h => h.ElectricityCo2) / 1000,
                                  ElectricityKwh = annual.Sum(h => h.ElectricityKwh),
                                  GasCo2 = annual.Sum(h => h.GasCo2) / 1000,
                                  GasKwh = annual.Sum(h => h.GasKwh),
                                  CO2 = annual.Sum(h => h.Co2) / 1000,
                                  MinDate = annual.Min(h => h.Date),

                                  EnergyRate = (annual.Sum(h => h.GasKwh) + annual.Sum(h => h.ElectricityKwh)) / (annual.Count() * 24)
                              });

        chartCo2.DataSource = allReadings;
        chartCo2.DataBind();

        dgData.DataSource = allReadings.OrderBy(all => all.Year);
        dgData.DataBind();

        var minYear = allReadings.Min(r => r.Year);
        var maxYear = allReadings.Max(r => r.Year);
        Page.Title = string.Format("CO2 emissions between {0} and {1}", minYear, maxYear);
    }

}
